package com.LGF.Book.biz;

import com.LGF.Book.bean.Member;
import com.LGF.Book.bean.MemberType;
import com.LGF.Book.bean.Record;
import com.LGF.Book.bean.UserType;
import com.LGF.Book.dao.MemberDao;
import com.LGF.Book.dao.MemberTypeDao;
import com.LGF.Book.dao.RecordDao;
import com.LGF.Book.dao.UserTypeDao;

import java.sql.SQLException;
import java.util.List;

public class MemberBiz {
    MemberDao memberDao = new MemberDao();
    UserTypeDao userTypeDao = new UserTypeDao();
    UserType userType = new UserType();
    MemberType memberType = new MemberType();
    MemberTypeDao memberTypeDao = new MemberTypeDao();
    RecordDao recordDao = new RecordDao();


    public int mem_modify(long memId,String mname,String sex,String tel,String idNumber){
        int count = 0;
        try {
            count = memberDao.mem_modify(memId, mname, sex, tel, idNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 调用dao层根据id 密码，用户类型id 获取会员信息
     * @param id
     * @param pwd
     * @param userId
     * @return
     */
    public Member getMember(long id, String pwd, long userId){
        Member member = null;
        try {
            member = memberDao.getMember(id, pwd, userId);
            //System.out.println(member.toString());
           // System.out.println("============");
            if (member!=null){
                ////根据类型编号获取类型对象
                userType = userTypeDao.getUserType(member.getUserId());
                memberType = memberTypeDao.getId(member.getTypeId());

                member.setPower(userType);
                member.setType(memberType);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return member;
    }

    /**
     * 调用dao层根据姓名、密码、身份证号查询会员信息
     * @param mname
     * @param pwd
     * @param idNumber
     * @return
     */
    public Member getMember(String mname,String pwd,String idNumber){
        Member member = null;
        try {
            member = memberDao.getMember(mname, pwd, idNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return member;

    }



    /**
     * 修改密码biz层
     * @param id
     * @param pwd
     * @return
     */
    public int modify(long id ,String pwd){

        int count = 0;
        try {
            count = memberDao.modify(id, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 调用dao层注册会员方法
     * @param mname
     * @param pwd
     * @param sex
     * @param typeId
     * @param balance
     * @param tel
     * @param idNumber
     * @return
     */

    public int register(String mname,String pwd,String sex,long typeId,double balance,String tel,String idNumber){
        int count = 0;
        try {
            count = memberDao.register(mname, pwd, sex, typeId, balance, tel, idNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 添加会员biz层
     * @param id
     * @param mname
     * @param pwd
     * @param sex
     * @param typeId
     * @param balance
     * @param tel
     * @param idNumber
     * @return
     */
    public int add(long id ,String mname,String pwd,String sex,long typeId,double balance,String tel,String idNumber)  {
        int count = 0;
        try {
            count = memberDao.add(id, mname, pwd, sex, typeId, balance, tel, idNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }

    /**
     * 获取所有会员信息biz层
     * @return
     */
    public List<Member> getAll(){
        List<Member> members = null;
        try {
            members = memberDao.getAll();
            //遍历会员对象
            for (Member member:members) {
                //根据类型编号获取类型对象
                MemberType type = memberTypeDao.getId(member.getTypeId());
                UserType userType = userTypeDao.getUserType(member.getUserId());
                member.setType(type);
                member.setPower(userType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return members;
    }

    /**
     * 根据id获取会员信息
     * @param id
     * @return
     */
    public Member getById(long id){
        Member member = null;
        try {
            member = memberDao.getById(id);
            if (member!=null){
                MemberType memberType = memberTypeDao.getId(member.getTypeId());
                UserType userType = userTypeDao.getUserType(member.getUserId());
                member.setType(memberType);
                member.setPower(userType);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return member;

    }

    /**
     * 分页功能
     * @param page pageIndex 第几页，从1开始
     * @param limit pageSize 每页多少行
     * @return 当前页的信息
     */
    public List<Member>  getByPage(int page,int limit){
        List<Member> members = null;
        try {
            members = memberDao.getByPage(page, limit);
            for (Member member:members) {
                //根据类型编号获取类型对象
                MemberType type = memberTypeDao.getId(member.getTypeId());
                UserType userType = userTypeDao.getUserType(member.getUserId());
                member.setType(type);
                member.setPower(userType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return members;
    }

    /**
     * 获取页码数
     * @param pageSize
     * @return
     */
    public int getPageCount( int pageSize){
        int pageCount = 0;
        try {
            //获取行数
            
          int   rowCount = memberDao.getCount();
          //根据行数得到页数，算出每页多少条
            pageCount =(rowCount-1)/pageSize+1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pageCount;
    }

    /**
     * 修改会员信息
     * @param id
     * @param mname
     * @param pwd
     * @param sex
     * @param typeId
     * @param balance
     * @param tel
     * @param idNumber
     * @return
     */
    public int mem_modify(long id,String mname,String pwd,String sex,long typeId,double balance,String tel,String idNumber)  {
        int count = 0;
        try {
            count = memberDao.mem_modify(id, mname, pwd, sex, typeId, balance, tel, idNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据会员id删除会员信息
     * @param id
     */
    public int mem_remove(long id){
        int count = 0;
        try {
            count = memberDao.mem_remove(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据ID模糊查询
     * @param id
     * @return
     */
    public List<Member> selectById(String id){
        List<Member> members = null;
        try {
            members = memberDao.selectById(id);
            //遍历会员对象
            for (Member member:members) {
                //根据类型编号获取类型对象
                MemberType type = memberTypeDao.getId(member.getTypeId());
                UserType userType = userTypeDao.getUserType(member.getUserId());
                member.setType(type);
                member.setPower(userType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return members;

    }

    /**
     * 根据会员姓名模糊查询
     * @param mname
     * @return
     */
    public List<Member> selectByMname(String mname){
        List<Member> members = null;
        try {
            members = memberDao.selectByMname(mname);
            //遍历会员对象
            for (Member member:members) {
                //根据类型编号获取类型对象
                MemberType type = memberTypeDao.getId(member.getTypeId());
                UserType userType = userTypeDao.getUserType(member.getUserId());
                member.setType(type);
                member.setPower(userType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return members;

    }

    /**
     * 给会员充值
     * @param memId
     * @param balance
     * @return
     */
    public int modifyBalance( long memId,double balance) {
        int count = 0;
        try {
            count = memberDao.modifyBalance(memId, balance);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }


        public static void main(String[] args) {
        MemberBiz  memberBiz = new MemberBiz();
//        int count = memberBiz.modify(2022000, "123");
//        System.out.println(count);
            List<Member> members = memberBiz.selectById("2022");
            System.out.println(members.toString());
        }
}
